package com.kool.kmetrics.metrics;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.bean.ReportClientNoAckCountDo;
import com.kool.kmetrics.constant.DateDimEnum;
import com.kool.kmetrics.consumer.vo.ClientNoAckSendPacketCnt;
import com.kool.kmetrics.consumer.vo.NoAckSendPacketLog;
import com.kool.kmetrics.dao.ReportClientNoAckCountDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luyu
 */
@Component
public class NoAckSendPacketComputer implements MetricsComputer {
    @Resource
    private ReportClientNoAckCountDao clientNoAckCountDao;

    @Override
    public void compute(String log) {
        NoAckSendPacketLog noAckSendPacketLog = JSON.parseObject(log, NoAckSendPacketLog.class);
        String timestamp = noAckSendPacketLog.getTimestamp();
        List<ClientNoAckSendPacketCnt> clientNoAckSendPacketCnts = noAckSendPacketLog.getClientNoAckSendPacketCnts();
        if (clientNoAckSendPacketCnts != null) {
            List<ReportClientNoAckCountDo> clientNoAckCountDos = new ArrayList<>();
            for (ClientNoAckSendPacketCnt clientNoAckSendPacketCnt : clientNoAckSendPacketCnts) {
                ReportClientNoAckCountDo clientNoAckCountDo = new ReportClientNoAckCountDo();
                clientNoAckCountDo.setDateDim(DateDimEnum.Day.getCode())
                        .setReportDate(Integer.parseInt(timestamp.substring(0, 8)))
                        .setClientId(clientNoAckCountDo.getClientId())
                        .setAvgCnt((long) clientNoAckSendPacketCnt.getCnt())
                        .setMaxCnt((long) clientNoAckSendPacketCnt.getCnt());

                clientNoAckCountDos.add(clientNoAckCountDo);
            }
            clientNoAckCountDao.batchInsert(clientNoAckCountDos);
        }

    }
}
